import java.util.Arrays;

public class Test {
    public static void main(String[] args) {
        int[] array = {1,12,31,4,5};
        System.out.println(Arrays.toString(array));
        bubbleSort(array);
        System.out.println(Arrays.toString(array));
    }
    public static void bubbleSort(int[] array) {
        for(int i = 0;i < array.length-1;i++) {
            boolean flg = false;

            for(int j = 0;j < array.length-1-i; j++) {
                if(array[j] > array[j+1]) {
                    int tmp = array[j];
                    array[j] = array[j+1];
                    array[j+1] = tmp;
                    flg = true;
                }
            }
            if(flg == false) {
                return;//说明已经有序了
            }
        }
    }
    public static void main11(String[] args) {
//        int[] array = new int[10];
//
//        Arrays.fill(array,1,3,99);
//        System.out.println(Arrays.toString(array));

       int[] array1 = {1,2,31,14,5};
//        int[] array2 = {1,2,31777,14,5};
//        System.out.println(Arrays.equals(array1, array2));//判断两个数组是否一样
//        //一样返回true,不一样返回false

        Arrays.sort(array1);
        System.out.println(Arrays.toString(array1));
    }
    public static void reverse(int[] array) {
        int i = 0;
        int j = array.length-1;
        while (i < j) {
            int tmp = array[i];
            array[i] = array[j];
            array[j] = tmp;
            i++;
            j--;
        }
    }
    //二分查找
    public static int binarySearch(int[] array,int key) {
        int i = 0;
        int j =array.length-1;
        while(i <= j) {
            int mid = (i+j) / 2;
            if(array[mid]<key) {
                i = mid + 1;
            }else if(array[mid] ==key) {
                return mid;
            }else{
                j = mid - 1;
            }
        }
        return -1;
    }
    public static int find(int[] array,int key) {
        for(int i= 0; i < array.length; i++) {
            if(array[i] == key) {
                return i;
            }
        }
        return -1;
    }
    public static void main10(String[] args) {
        int[] array = {1,2,3,4,5};
        int[] dest = new int[array.length];
        System.arraycopy(array,0,dest,0,array.length);
        System.out.println(Arrays.toString(dest));

    }
    public static void main9(String[] args) {
        int[] array = {1,2,3,4,5};
        //拷贝 array 这个数组  ，新的长度为 array.length *2
        array = Arrays.copyOf(array,array.length*2);
        //array 指向了 新的数组空间
        System.out.println(Arrays.toString(array));

    }
    public static void main8(String[] args) {
        int[] array = {1,2,3,4};
        //拷贝 array 这个数组  ，长度为 array.length
        int[] copy = Arrays.copyOf(array,array.length);

        System.out.println(Arrays.toString(copy));

    }
    public static void main7(String[] args) {
        int[] array = {1,2,3,4};
        String ret = myToString(array);
        System.out.println(ret);
    }
    public static String myToString(int[] array) {
        assert array != null;

        if(array == null) {
            return "null";
        }
        String ret = "[";
        for(int i = 0;i < array.length;i++) {
            ret +=array[i];
            if(i != array.length-1) {
                ret +=",";
            }
        }
        ret+="]";
        return ret;
    }
    public static void main6(String[] args) {
        int[] array = {1,2,3,4};
        for(int i = 0; i < array.length;i++) {
            System.out.print(array[i]+" ");
        }
        System.out.println();

        for (int x:array) {
            System.out.print(x+" ");
        }
        System.out.println();

        String ret = Arrays.toString(array);
        System.out.println(ret);
    }

    public static String myTostring(int[] arr){
        String ret ="[";
        int i = 0;
        for(i = 0;i < arr.length;i++)
        {
            if(i < arr.length-1) {
                ret +=arr[i] + ",";
            }else if(i == arr.length-1){
                ret +=arr[i]+"]";
            }
        }
        return ret;
    }
    public static void main5(String[] args) {
        int[] arr = {1,2,3,4,5,6};
        String ret = myTostring(arr);
        System.out.println(ret);
    }
    public static int summ(int n) {
        if(n < 10) {
            return n;
        }
        return n%10+summ(n/10);
    }

    public static void main4(String[] args) {
        System.out.println(summ(1234));
    }
    public static void prints(int n) {
        if(n<10) {
            System.out.print(n+" ");
            return;
        }
        prints(n/10);
        System.out.print(n%10+" ");
    }
    public static void main3(String[] args) {
        prints(1234);
    }
    public static int sum(int n) {
        if(n == 1) return 1;
        return n+sum(n-1);
    }

    public static void main2(String[] args) {
        System.out.println(sum(10));
    }
    public static int fun(int n) {
        if(n == 1)return 1;
        return n*fun(n-1);
    }
    public static void main1(String[] args) {
        System.out.println(fun(5));
    }
}
